package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.bean.PatrolBean;
import com.cz.nongyetongv5.bean.PatrolContent;
import com.cz.nongyetongv5.bean.PatrolPic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PatrolDetailActivity extends Activity implements View.OnClickListener {


    private List<PatrolPic> patrolPics;
    private PhotoAdapter adapter;
    private RelativeLayout btnBack;
    private LinearLayout ll_bottom;
    private RecyclerView lv_patrol_pic;
    private PatrolBean patrolBean;
    private TextView tv_type;
    private TextView tv_addr;
    private TextView tv_obj;
    private TextView tv_record;
    private TextView tv_admin;
    private TextView tv_date;

    private String add_user;
    private String region_code;
    private TextView tv_pass;


    private RelativeLayout rl_patrol_content;
    private TextView tv_content;
    private TextView tv_title;
    private int type = 0;
    private RelativeLayout rl_addr;
    private RelativeLayout rl_object;
    private RelativeLayout rl_record;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patrol_detail);
        patrolBean = (PatrolBean) getIntent().getSerializableExtra("patrolBean");
        add_user = getIntent().getStringExtra("add_user");
        region_code = getIntent().getStringExtra("region_code");
        type = getIntent().getIntExtra("type", 0);
        patrolPics = patrolBean.getPhoto();
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        ll_bottom = findViewById(R.id.ll_bottom);
        ll_bottom.setOnClickListener(this);
        lv_patrol_pic = findViewById(R.id.lv_patrol_pic);
        tv_pass = findViewById(R.id.tv_pass);
        tv_pass.setOnClickListener(this);
        tv_addr = findViewById(R.id.tv_addr);
        tv_obj = findViewById(R.id.tv_obj);
        tv_record = findViewById(R.id.tv_record);
        tv_admin = findViewById(R.id.tv_admin);
        tv_date = findViewById(R.id.tv_date);
        tv_type = findViewById(R.id.tv_type);
        tv_content = findViewById(R.id.tv_content);
        rl_patrol_content = findViewById(R.id.rl_patrol_content);
        rl_patrol_content.setOnClickListener(this);


        rl_addr= findViewById(R.id.rl_addr);
        rl_object= findViewById(R.id.rl_object);
        rl_record= findViewById(R.id.rl_record);



        tv_title = findViewById(R.id.tv_title);
        if (type == 1) {
            tv_title.setText("日常巡查详情");
            ll_bottom.setVisibility(View.GONE);
        }

        tv_type.setText(patrolBean.getType_name());

        if (patrolBean.getType() != null && patrolBean.getType().equals("1")) {

            rl_object.setVisibility(View.VISIBLE);
            rl_patrol_content.setVisibility(View.VISIBLE);
            rl_addr.setVisibility(View.GONE);
            rl_record.setVisibility(View.GONE);
            tv_obj.setText(patrolBean.getEnt_name());
            tv_content.setText(patrolBean.getContent());
        } else {
            rl_object.setVisibility(View.GONE);
            rl_patrol_content.setVisibility(View.GONE);
            rl_addr.setVisibility(View.VISIBLE);
            rl_record.setVisibility(View.VISIBLE);
            tv_addr.setText(patrolBean.getCheck_address());
            tv_record.setText(patrolBean.getCheck_record());
        }

        tv_admin.setText(patrolBean.getRealname());
        tv_date.setText(patrolBean.getAdd_time());


        adapter = new PhotoAdapter();
        lv_patrol_pic.setLayoutManager(new GridLayoutManager(PatrolDetailActivity.this, 3));
        lv_patrol_pic.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(PatrolDetailActivity.this, PicDetailActivity.class);
                intent.putExtra("position", position);
                intent.putExtra("photos", (Serializable) patrolPics);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                finish();
                break;
            case R.id.tv_pass:
                Intent intent = new Intent(PatrolDetailActivity.this, DialogActivity.class);
                intent.putExtra("add_user", add_user);
                intent.putExtra("region_code", region_code);
                intent.putExtra("ids", patrolBean.getCheck_id());
                intent.putExtra("model", "patrol");
                startActivityForResult(intent, 9004);
                break;
            case R.id.rl_patrol_content:
                //TODO：巡查内容
                List<List<PatrolContent>> patrolContents = new ArrayList<>();
                Intent intent1 = new Intent(PatrolDetailActivity.this, PatrolContentDetailActivity.class);
                if (patrolBean != null) {
                    if (patrolBean.getPesticideList() != null) {
                        patrolContents.add(patrolBean.getPesticideList());
                    }
                    if (patrolBean.getProcessList() != null) {
                        patrolContents.add(patrolBean.getProcessList());
                    }
                    if (patrolBean.getQualityList() != null) {
                        patrolContents.add(patrolBean.getQualityList());
                    }
                    if (patrolBean.getTrainList() != null) {
                        patrolContents.add(patrolBean.getTrainList());
                    }
                }

                intent1.putExtra("checkContent", (Serializable) patrolContents);
                startActivity(intent1);

                break;
        }
    }


    public class PhotoAdapter extends BaseQuickAdapter<PatrolPic, BaseViewHolder> {
        public PhotoAdapter() {
            super(R.layout.product_pic_item, patrolPics);
        }

        @Override
        protected void convert(BaseViewHolder helper, PatrolPic item) {
            Glide.with(mContext)
                    .load(item.getAttach_url()).placeholder(R.drawable.loading)
                    .into((ImageView) helper.getView(R.id.iv_iv));
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 9004:
                    setResult(RESULT_OK);
                    finish();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
